<?php
declare (strict_types=1);

namespace app\course\logic\cert\admin;

use app\course\logic\base\Course;
use app\course\model\base\CourseSection;
use app\course\model\cert\admin\CourseCert as CourseCertModel;
use app\course\model\classes\admin\ClassesLink;
use app\user\logic\user\admin\Account;

/**
 * Class CourseCert
 *
 * @package app\course\logic\cert\admin
 */
class CourseCert extends CourseCertModel
{
    /**
     * 某课程的学员列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/10/13
     *
     * @param int   $course_id
     * @param array $map
     * @param null  $phone
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getStudentList(int $course_id, array $map = [], $phone = null)
    {
        $builder = $this->with(['user']);
        if ($phone) {
            // withTrashed
            // $user_id = Account::withoutGlobalScope(['mhm'])->where('phone', $phone)->removeOption('soft_delete')->value('id');
            $user_id = Account::where('phone', $phone)->removeOption('soft_delete')->value('id');
            $builder = $builder->where('user_id', $user_id);
        }

        return $builder->where('course_id', $course_id)->where($map)->paginate($this->getListRows());
    }

    /**
     * 获取学员的打印数据信息
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-07
     *
     * @param int $id        [description]
     * @param int $user_id   [description]
     * @param int $course_id [description]
     * @return   [type]                                    [description]
     */
    public function getStudentPrintData(int $user_id, int $course_id, $show_sections)
    {
        $info = $this->with(['user', 'course'])->where('user_id', $user_id)->where('course_id', $course_id)->findOrFail()->withAttr('cert_create_time', function ($value, $data) {
            return date('Y-m-d', $data['create_time']);
        })->append(['cert_create_time']);

        // 查询课时类型
        $course_type = Course::withTrashed()->where('id', $info->course_id)->value('course_type');
        if ($course_type == 4) {
            // 班级课,查询课程名称
            // 获取所有课程名称
            $show_sections && $info->course_list = ClassesLink::where('classes_id', $info->course_id)->withJoin(['course' => ['title']])->field(['course.title'])->select();
            // 班级课所有课程
            $ids                       = ClassesLink::where('classes_id', $info->course_id)->column('course_id');
            $info->finished_class_hour = CourseSection::whereIn('course_id', $ids)->where('is_last', 1)->sum('class_hour');
        } else {
            // 查询第一级课时名称
            $show_sections && $info->course_list = CourseSection::where('course_id', $info->course_id)->where('pid', 0)->order('create_time', 'ASC')->field('title')->select();
            $info->finished_class_hour = CourseSection::where('course_id', $info->course_id)->where('is_last', 1)->sum('class_hour');
        }

        return $info;
    }

    /**
     * 学员证书
     */
    public function getUserCerts(array $map = [], int $userId = 0)
    {
        return $this->withJoin(['course'])->where('__TABLE__.user_id', $userId)->where($map)->paginate($this->getListRows());
    }
}